package com.yeyouluo.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

public class HdfsClient {
    private FileSystem fs;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // 配置在集群上运行
        // configuration.set("fs.defaultFS", "hdfs://linux121:9000");
        // FileSystem fs = FileSystem.get(configuration);
        fs = FileSystem.get(new URI("hdfs://linux121:9000"),
                configuration, "hadoop");
    }

    /**
     * 创建目录
     *
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void testMkdirs() throws IOException {
        fs.mkdirs(new Path("/test"));
    }

    /**
     * 上传文件
     *
     * @throws IOException
     */
    @Test
    public void testCopyFromLocalFile() throws IOException {
        fs.copyFromLocalFile(new Path("D:/lagou.txt"), new Path("/test"));
    }

    /**
     * 下载文件
     *
     * @throws IOException
     */
    @Test
    public void testCopyToLocalFile() throws IOException {
//        fs.copyToLocalFile(new Path("/test/lagou.txt"),new Path("D:/lagou-copy.txt"));
        fs.copyToLocalFile(false, new Path("/test/lagou.txt"), new Path("D:/lagou-copy.txt"), true);
    }

    /**
     * 删除目录/文件
     *
     * @throws IOException
     */
    @Test
    public void testDelete() throws IOException {
//        fs.delete(new Path("/test/lagou.txt"), true);
        fs.delete(new Path("/test"), true);
    }

    /**
     * 文件详情
     *
     * @throws IOException
     */
    @Test
    public void testlistFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fs.listFiles(new Path("/"), true);
        while (locatedFileStatusRemoteIterator.hasNext()){
            LocatedFileStatus fileStatus = locatedFileStatusRemoteIterator.next();
            System.out.println("isFile=" + fileStatus.isFile());
            System.out.println("isDirectory=" + fileStatus.isDirectory());
            System.out.println("getPathName=" + fileStatus.getPath().getName());
            System.out.println("getBlockSize=" + fileStatus.getBlockSize());
            System.out.println("getLen=" + fileStatus.getLen());
            System.out.println("getReplication=" + fileStatus.getReplication());
            System.out.println("getPermission=" + fileStatus.getPermission());
            System.out.println("getGroup=" + fileStatus.getGroup());
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            Arrays.stream(blockLocations).forEach(x -> {
                try {
                    System.out.println(Arrays.toString(x.getHosts()) + "\t" + Arrays.toString(x.getNames()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            System.out.println("------------------------------------\n");
        }

    }

    public void destory() throws IOException {
        // 3 关闭资源
        fs.close();

    }

}
